<template>
  <el-dialog
    title="添加页面"
    :visible="showDialog"
    @close="closeForm"
  >
    <el-form ref="ruleForm" :model="formData" :rules="rules" label-width="100px">
      <el-form-item label="标题" prop="title">
        <el-input v-model="formData.title" />
      </el-form-item>
      <el-form-item label="内容" prop="content">
        <quillEditor v-model="formData.content" @blur="$refs.ruleForm.validateField('content')" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeForm">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'
import { addPagesList, editPagesList } from '@/api/pages/index'
export default {
  components: { quillEditor },
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formData: {
        content: '',
        title: ''

      },
      rules: {
        title: [{ required: true, message: '标题不能为空', trigger: 'blur' }],
        content: [{ required: true, message: '内容不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    // 新增页面
    async submit() {
      await this.$refs.ruleForm.validate()
      if (this.formData.id) {
        await editPagesList(this.formData)
      } else {
        await addPagesList(this.formData)
      }
      this.$emit('updatedata')
      this.closeForm()
    },
    // 关闭弹窗
    closeForm() {
      this.$refs.ruleForm.resetFields()
      this.formData = {
        title: '',
        content: ''
      }
      this.$emit('update:showDialog', false)
    }
  }

}
</script>

<style>

</style>
